﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuuntoAmbitConnector
{
    public class Workout
    {
        private long durationInSeconds;

        public Workout(DateTime startDate, int activity, long durationInSeconds, string comment)
        {
            this.InitializeWorkout(startDate, activity, durationInSeconds, comment);
        }

        public Workout(DateTime startDate, int activity, int hourPart, int minutePart, int secondPart, string comment)
        {
            if (hourPart < 0 || minutePart < 0 || secondPart < 0)
                throw new ArgumentOutOfRangeException("hourPart|minutePart|secondPart", 
                    "Hours, minutes and seconds cannot be less than zero.");

            var seconds = hourPart * 60 * 60 + minutePart * 60 + secondPart;

            this.InitializeWorkout(startDate, activity, seconds, comment);
        }

        private void InitializeWorkout(DateTime startDate, int activity, long durationInSeconds, string comment)
        {
            this.StartDate = startDate;
            this.Activity = activity;
            this.DurationInSeconds = durationInSeconds;
            this.Comment = comment;
        }

        public DateTime StartDate { get; set; }

        public int Activity { get; set; }
                
        public long DurationInSeconds 
        {
            get
            {
                return durationInSeconds;
            }

            set
            {
                if (value < 0)
                    throw new ArgumentOutOfRangeException("durationInSeconds",
                        "Duration cannot be less than zero.");
                durationInSeconds = value;
            }
        }

        public long DistanceInMeters { get; set; }

        public string Comment { get; set; }

        public int GetDurationHourPart()
        {
           return (int) this.DurationInSeconds / 60 / 60;
        }

        public int GetDurationMinutePart() 
        {            
            var minutes = this.DurationInSeconds / 60;
            var hoursRestMinutes = minutes % 60;

            return (int) (hoursRestMinutes);
        }

        public int GetDurationSecondPart()
        { 
            return (int) this.DurationInSeconds % 60;            
        }

        public override string ToString()
        {
            StringBuilder workoutString = new StringBuilder();
            workoutString.AppendLine(string.Format("Start date: {0}", this.StartDate.ToString("yyyy-MM-dd")));
            workoutString.AppendLine(string.Format("Activity: {0}", this.Activity));
            workoutString.AppendLine(string.Format("Duration: {0}h {1}min {2}sec", this.GetDurationHourPart(), this.GetDurationMinutePart(), this.GetDurationSecondPart()));
            if (this.DistanceInMeters > 0)
                workoutString.AppendLine(string.Format("Distance: {0}km {1}m", this.GetDistanceKilometerPart(), this.GetDistanceMeterPart()));
            workoutString.AppendLine(string.Format("Comment: {0}", this.Comment));

            return workoutString.ToString();
        }



        public int GetDistanceKilometerPart()
        {
            return (int)this.DistanceInMeters / 1000;
        }

        public int GetDistanceMeterPart()
        {
            return (int)this.DistanceInMeters % 1000;
        }
    }
}
